package edu.snake.implement.model.arena.iterator;

import edu.snake.core.model.arena.Arena;
import edu.snake.core.model.arena.Position;
import edu.snake.implement.model.arena.Direction;

import java.util.Iterator;

import static edu.snake.implement.model.arena.PositionFactory.getNewPosition;


public class SimpleArenaIterator implements Iterator<Position> {
    private final Arena arena;
    private Position position;

    public SimpleArenaIterator(Arena arena) {
        this.arena = arena;
        position = getNewPosition(-1, 0);
    }

    @Override
    public boolean hasNext() {
        if (arena.checkArenaBorder(getNewPosition(position, Direction.RIGHT))) {
            if (arena.checkArenaBorder(getNewPosition(position, Direction.DOWN))) {
                return false;
            }
        }
        return true;
    }

    @Override
    public Position next() {
        Position result = getNewPosition(position, Direction.RIGHT);
        if (result.getWidth() >= arena.getWidth()) {
            result = getNewPosition(0, position.getHeight() + 1);
        }
        position = result;
        return result;
    }
}
